import 'dart:convert' show json;
import 'package:flutter/cupertino.dart';
import 'base_chart_axis.dart';
import 'base_chart_formatter.dart';
import 'base_model_asT.dart';
import 'package:ribo_base_core/base_styles/base_chart_style.dart';

class BaseChartStackBarsLinesModel {
  /// https://echarts.apache.org/zh/option.html#tooltip.formatter
  BaseChartFormatter tooltipFormatter;

  /// https://echarts.apache.org/zh/option.html#series-pie.label.formatter
  BaseChartFormatter labelFormatter;

  /// 数据轴上的formatter
  BaseChartFormatter axisLabelFormatter;

  /// 折线图的axisLabelFormatter
  BaseChartFormatter lineAxisLabelFormatter;

  /// 柱状条是否水平，默认垂直显示
  bool isHorizontal;

  /// 柱形图数据轴上的最小值
  double barAxisMin;

  /// 柱形图数据轴上的最大值
  double barAxisMax;

  /// 柱形图数据轴上的最小值
  double lineAxisMin;

  /// 柱形图数据轴上的最大值
  double lineAxisMax;

  /// 折线使用的axis index
  int lineAxisIndex;

  /// 柱状图使用的axis index
  int barAxisIndex;

  /// 坐标轴显示设置
  BaseChartAxis axis;

  /// 图表样式
  BaseChartStyle style;

  /// https://echarts.apache.org/zh/option.html#series-line.markLine
  Map<String, dynamic> markLinke;

  /// https://echarts.apache.org/zh/option.html#dataZoom
  Map<String, dynamic> dataZoom;

  /// 图表数据
  List<BaseChartStackBarsLinesSeries> series;

  BaseChartStackBarsLinesModel({
    this.tooltipFormatter,
    this.labelFormatter,
    this.axisLabelFormatter,
    this.lineAxisLabelFormatter,
    this.isHorizontal = false,
    this.lineAxisIndex = 0,
    this.barAxisIndex = 0,
    this.barAxisMin,
    this.barAxisMax,
    this.lineAxisMin,
    this.lineAxisMax,
    this.style,
    this.axis,
    this.dataZoom,
    this.markLinke,
    @required this.series,
  }) : assert(series != null);

  factory BaseChartStackBarsLinesModel.fromJson(Map<String, dynamic> jsonRes) {
    if (jsonRes == null) {
      return null;
    }

    final List<BaseChartStackBarsLinesSeries> series =
        jsonRes['data'] is List ? <BaseChartStackBarsLinesSeries>[] : null;
    if (series != null) {
      for (final dynamic item in jsonRes['data']) {
        if (item != null) {
          series.add(BaseChartStackBarsLinesSeries.fromJson(
              asT<Map<String, dynamic>>(item)));
        }
      }
    }

    return BaseChartStackBarsLinesModel(
      tooltipFormatter: asT<BaseChartFormatter>(jsonRes['tooltipFormatter']),
      labelFormatter: asT<BaseChartFormatter>(jsonRes['labelFormatter']),
      axisLabelFormatter:
          asT<BaseChartFormatter>(jsonRes['axisLabelFormatter']),
      lineAxisLabelFormatter:
          asT<BaseChartFormatter>(jsonRes['lineAxisLabelFormatter']),
      isHorizontal: asT<bool>(jsonRes['isHorizontal']),
      barAxisMin: asT<double>(jsonRes['barAxisMin']),
      barAxisMax: asT<double>(jsonRes['barAxisMax']),
      lineAxisMin: asT<double>(jsonRes['lineAxisMin']),
      lineAxisMax: asT<double>(jsonRes['lineAxisMax']),
      lineAxisIndex: asT<int>(jsonRes['lineAxisIndex']),
      barAxisIndex: asT<int>(jsonRes['barAxisIndex']),
      dataZoom: asT<Map<String, dynamic>>(jsonRes['dataZoom']),
      axis: asT<BaseChartAxis>(jsonRes['axis']),
      style: asT<BaseChartStyle>(jsonRes['style']),
      markLinke: asT<Map<String, dynamic>>(jsonRes['markLinke']),
      series: series,
    );
  }

  Map<String, dynamic> toJson() => <String, dynamic>{
        'tooltipFormatter': tooltipFormatter,
        'labelFormatter': labelFormatter,
        'axisLabelFormatter': axisLabelFormatter,
        'lineAxisLabelFormatter': lineAxisLabelFormatter,
        'barAxisIndex': barAxisIndex,
        'isHorizontal': isHorizontal,
        'barAxisMin': barAxisMin,
        'barAxisMax': barAxisMax,
        'lineAxisMin': lineAxisMin,
        'lineAxisMax': lineAxisMax,
        'axis': axis,
        'style': style,
        'dataZoom': dataZoom,
        'series': series,
        'markLinke': markLinke
      };

  @override
  String toString() {
    return json.encode(this);
  }
}

class BaseChartStackBarsLinesSeries {
  BaseChartStackBarsLinesSeries(
      {String name, this.color, this.stackName = 'bar1', this.data, this.isLine = false, this.isGradient = false, this.gradientColor})
      : this.name = name.startsWith("20")?name.replaceRange(0, 2, ""):name; //只显示最后的日期

  factory BaseChartStackBarsLinesSeries.fromJson(
          Map<String, dynamic> jsonRes) =>
      jsonRes == null
          ? null
          : BaseChartStackBarsLinesSeries(
              name: asT<String>(jsonRes['name']),
              data: asT<List<BaseChartStackBarsLinesData>>(jsonRes['data']),
              isLine: asT<bool>(jsonRes['isLine']),
              color: asT<String>(jsonRes['color']),
              stackName: asT<String>(jsonRes['stackName']),
            );

  /// 数据名称
  String name;

  /// 颜色
  String color;

  /// 堆叠名称
  String stackName;

  /// 是否是折线图，为否则为柱状图
  bool isLine;

  /// 数据值
  List<BaseChartStackBarsLinesData> data;

  ///是渐变(目前仅用于柱状图，折线图不知道有没有效果)
  bool isGradient;

  ///渐变颜色 BaseChartFormatter.fromFunction("function(){return new echarts.graphic.LinearGradient(0, 0, 0, 1,[{offset: 0, color: '#036EB8'},{offset: 1, color: '#00A29A'}])}")
  BaseChartFormatter gradientColor;

  Map<String, dynamic> toJson() => <String, dynamic>{
        'name': name,
        'data': data,
        'color': color,
        'stackName': stackName,
        'isLine': isLine,
        'isGradient': isGradient,
        'gradientColor': gradientColor,
      };

  @override
  String toString() {
    return json.encode(this);
  }
}

class BaseChartStackBarsLinesData {
  BaseChartStackBarsLinesData({
    String name,
    this.value,
  }) : this.name = name.startsWith("20")?name.replaceRange(0, 2, ""):name; //只显示最后的日期

  factory BaseChartStackBarsLinesData.fromJson(Map<String, dynamic> jsonRes) =>
      jsonRes == null
          ? null
          : BaseChartStackBarsLinesData(
              name: asT<String>(jsonRes['name']),
              value: asT<double>(jsonRes['value']),
            );

  /// 数据名称
  String name;

  /// 数据值
  double value;

  Map<String, dynamic> toJson() => <String, dynamic>{
        'name': name,
        'value': value,
      };

  @override
  String toString() {
    return json.encode(this);
  }
}
